package com.ddwl.goods.controller.retail;

import com.ddwl.common.annotation.Auth;
import com.ddwl.common.constant.BaseEnum;
import com.ddwl.common.controller.BaseController;
import com.ddwl.goods.service.retail.ShopItemCategoryService;
import com.ddwl.schema.bo.goods.ShopItemDefaultCategoryBo;
import com.ddwl.schema.bo.goods.retail.*;
import com.ddwl.schema.vo.base.RestVo;
import com.ddwl.schema.vo.goods.retail.ShopItemCategoryListVo;
import com.ddwl.schema.vo.goods.retail.ShopItemCategoryPageVo;
import com.ddwl.schema.vo.goods.retail.ShopItemCategoryVo;
import com.ddwl.schema.vo.shop.ShopVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * @Author hemiaojun
 * @Date 2020/11/30 16:06
 */
@RestController
@RequestMapping("/shopItemCategory")
@Api(tags = "店铺商品分类")
public class ShopItemCategoryController extends BaseController {

    @Resource
    private ShopItemCategoryService shopItemCategoryService;

    @PostMapping("/add")
    @Auth(product = {BaseEnum.ProductEnum.SHOP_CONSOLE})
    @ApiOperation("新增店铺商品分类")
    public RestVo add(@Valid @RequestBody ShopItemCategoryBo bo){
        shopItemCategoryService.addShopItemCategory(bo, getShopId());
        return RestVo.ok();
    }

    @PostMapping("/addDefaultCategoryForApi")
    @Auth(product = {BaseEnum.ProductEnum.API})
    @ApiOperation("店铺审核通过新增默认商品分类")
    public RestVo addDefaultCategoryForApi(@Valid @RequestBody ShopItemDefaultCategoryBo bo){
        shopItemCategoryService.addDefaultCategory(bo);
        return RestVo.ok();
    }

    @PostMapping("/update")
    @Auth(product = {BaseEnum.ProductEnum.SHOP_CONSOLE})
    @ApiOperation("修改店铺商品分类")
    public RestVo update(@Valid @RequestBody ShopItemCategoryUpdateBo bo){
        shopItemCategoryService.updateShopItemCategory(bo,getShopId());
        return RestVo.ok();
    }

    @GetMapping("/get/{id}")
    @Auth(product = {BaseEnum.ProductEnum.SHOP_CONSOLE, BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE})
    @ApiOperation("店铺商品分类详情")
    public RestVo<ShopItemCategoryVo> get(@PathVariable("id") String id){
        return RestVo.ok(shopItemCategoryService.getShopItemCategory(id));
    }

    @DeleteMapping("/delete/{id}")
    @Auth(product = {BaseEnum.ProductEnum.SHOP_CONSOLE, BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE})
    @ApiOperation("删除店铺商品分类")
    public RestVo delete(@PathVariable("id") String id,@RequestParam(value = "shopId",required = false) String shopId){
        if (BaseEnum.ProductEnum.WBM.getName().equals(getProduct())) {
            getShopVo(shopId);
        }
        shopItemCategoryService.deleteShopItemCategory(id);
        return RestVo.ok();
    }

    @PostMapping("/effect")
    @Auth(product = {BaseEnum.ProductEnum.SHOP_CONSOLE, BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE})
    @ApiOperation("店铺商品分类启用禁用")
    public RestVo effect(@Valid @RequestBody ShopItemCategoryEffectBo bo){
        if (BaseEnum.ProductEnum.WBM.getName().equals(getProduct())) {
            getShopVo(bo.getShopId());
        }
        shopItemCategoryService.effect(bo);
        return RestVo.ok();
    }

    @PostMapping("/sort")
    @Auth(product = {BaseEnum.ProductEnum.SHOP_CONSOLE})
    @ApiOperation("店铺商品分类排序")
    public RestVo sort(@Valid @RequestBody ShopItemCategorySortBo bo){
        shopItemCategoryService.sort(bo);
        return RestVo.ok();
    }

    @PostMapping("/tree")
    @Auth(product = {BaseEnum.ProductEnum.SHOP_CONSOLE})
    @ApiOperation("店铺商品分类分页")
    public RestVo<List<ShopItemCategoryPageVo>> tree(@RequestBody ShopItemCategorySearchBo bo){
        if (null == bo) {
            bo = new ShopItemCategorySearchBo();
        }
        return RestVo.ok(shopItemCategoryService.tree(bo,getShopId()));
    }

    @GetMapping("/all")
    @Auth(product = {BaseEnum.ProductEnum.SHOP_CONSOLE})
    @ApiOperation("添加商品时选择分类")
    public RestVo<List<ShopItemCategoryVo>> all(){
        return RestVo.ok(shopItemCategoryService.all(getShopId()));
    }

    @GetMapping("/list/{shopId}")
    @ApiOperation("五八马小程序店铺商品分类展示")
    public RestVo<List<ShopItemCategoryListVo>> list(@PathVariable("shopId") String shopId){
        return RestVo.ok(shopItemCategoryService.list(shopId));
    }

    @GetMapping("/listForConsole")
    @Auth(product = {BaseEnum.ProductEnum.CONSOLE})
    @ApiOperation("运营后台商家商品分类")
    public RestVo<List<ShopItemCategoryVo>> listForConsole(@RequestParam("shopId") String shopId){
        return RestVo.ok(shopItemCategoryService.all(shopId));
    }

    @PostMapping("/merchant/add")
    @Auth(product = {BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE})
    @ApiOperation("五八马小程序商家端 新增商品分类")
    public RestVo<ShopItemCategoryVo> addForMerchant(@Valid @RequestBody MerchantShopItemCategoryBo bo){
        return RestVo.ok(shopItemCategoryService.addForMerchant(bo));
    }

    @PostMapping("/merchant/rename")
    @Auth(product = {BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE})
    @ApiOperation("五八马小程序商家端 商品分类重命名")
    public RestVo rename(@Valid @RequestBody ShopItemCategoryRenameBo bo){
        ShopVo shopVo= getShopVo(bo.getShopId());
        bo.setShopId(shopVo.getId());
        shopItemCategoryService.rename(bo);
        return RestVo.ok();
    }

    @GetMapping("/merchant/list")
    @ApiOperation("五八马小程序商家端 商品分类列表")
    @Auth(product = {BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE})
    public RestVo<List<ShopItemCategoryListVo>> listForMerchant(@RequestParam(required = false) String shopId){
        ShopVo shopVo= getShopVo(shopId);
        return RestVo.ok(shopItemCategoryService.listForMerchant(shopVo.getId()));
    }

    @GetMapping("/merchant/select")
    @ApiOperation("五八马小程序商家端 商品分类下拉选择")
    @Auth(product = {BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE})
    public RestVo<List<ShopItemCategoryListVo>> selectList(@RequestParam(required = false) String shopId){
        ShopVo shopVo= getShopVo(shopId);
        return RestVo.ok(shopItemCategoryService.selectList(shopVo.getId()));
    }

    @PostMapping("/merchant/sort/{shopId}")
    @ApiOperation("五八马小程序商家端 商品分类排序")
    @Auth(product = {BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE})
    public RestVo sortForMerchant(@PathVariable("shopId") String shopId,@ApiParam(value = "商品分类id数组(同一父类下的所有分类)，按顺序排列", required = true) @RequestBody List<String> ids){
        getShopVo(shopId);
        shopItemCategoryService.sortForMerchant(ids);
        return RestVo.ok();
    }

}
